﻿#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include <time.h>

//main154&155：文本文件写入和读取500个100内的随机数
int main154()
{
	srand((unsigned int)time(NULL));
	FILE* fp = fopen("../../../file/FileIn100RandomNumWrite.txt", "w");
	if (!fp)
	{
		return -1;
	}
	char buf[5];
	for (int i = 0; i < 500; i++)
	{
		//printf("%d-----%d\n", i + 1, rand() % 100 + 1);
		//fprintf(fp, "%d\n", rand() % 100 + 1);
		memset(buf, 0, 5);
		sprintf(buf, "%d\n", rand() % 100 + 1);
		printf("%d-------%s", i + 1, buf);//1-------87 ... 500-------54
		fputs(buf, fp);
	}
	fclose(fp);

	system("pause");
	return EXIT_SUCCESS;
}
int main155(void)
{
	FILE* fp = fopen("../../../file/FileIn100RandomNumWrite.txt", "r");
	if (!fp)
	{
		return -1;
	}
	char buf[5];
	int a[1024] = { 0 };

	/*for (int i = 0; i < 500; i++)
	{
		memset(buf, 0, 5);
		fgets(buf, 5, fp);
		//printf("%s", buf);
		sscanf(buf, "%d\n", &a[i]);
		//printf("%d\n", a[i]);
	}*/

	int i = 0;
	while (!feof(fp))
	{
		memset(buf, 0, 5);
		fgets(buf, 5, fp);
		sscanf(buf, "%d\n", &a[i++]);
		//printf("%d\n", a[i++]);
		//i++;
	}
	for (int i = 0; i < 500; i++)
	{
		printf("%d-----%d\n", i, a[i]);//0-----87 ... 499-----54
	}
	return 0;
}